package com.onLineCar.management.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onLineCar.common.core.constants.RabbitMQConstants;
import com.onLineCar.management.config.TagCass;
import com.onLineCar.management.domain.Mailbox;
import com.onLineCar.management.domain.TDriver;
import com.onLineCar.management.domain.TShortMessage;
import com.onLineCar.management.domain.RequestOrder;
import com.onLineCar.management.domain.request.MessageRequest;
import com.onLineCar.management.mapper.TShortMessageMapper;
import com.onLineCar.management.service.ITShortMessageService;
import com.onLineCar.management.template.OrderDataFeign;
import com.onLineCar.management.template.QueryDriverFeign;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import static com.onLineCar.management.config.UniformConstant.CONSTANT_ZERO;


/**
 * 短信模板表 服务实现类
 * </p>
 *
 * @author dsr
 * @since 2023-12-12
 */
@Service
public class TShortMessageServiceImpl extends ServiceImpl<TShortMessageMapper, TShortMessage> implements ITShortMessageService {

    @Autowired
    private TShortMessageMapper tShortMessageMapper;

    @Autowired
    private OrderDataFeign orderDataFeign;

    @Resource
    private QueryDriverFeign queryDriverFeign;


    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 根据tag查询短信模板
     *
     * @param messageRequest
     * @return
     */
    @Override
    public void selectTShortMessageByTag(MessageRequest messageRequest) {
        // 根据标签筛选TShortMessage
        LambdaQueryWrapper<TShortMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TShortMessage::getMarkers, messageRequest.tag);
        // 根据筛选条件获取TShortMessage列表
        List<TShortMessage> tShortMessages = tShortMessageMapper.selectList(wrapper);
        // 获取第一个TShortMessage对象
        TShortMessage tShortMessage = tShortMessages.get(CONSTANT_ZERO);
        // 获取TShortMessage的shortMessageName
        String shortMessageName = tShortMessage.getShortMessageName();
        // 创建一个LinkedHashMap用于存储替换后的参数
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        // 根据orderNumber获取TblOrder对象
        RequestOrder orderByOrderId = orderDataFeign.getOrderByOrderId(messageRequest.orderNumber);
        // 根据orderId获取Driver对象
        TDriver driver = queryDriverFeign.getDriver(orderByOrderId.getDriverId());
        // 创建Mailbox对象
        Mailbox messageRequests = new Mailbox();
        // 接单发给用户的
        if (TagCass.TAG_CASS_NAME.getName().equals(messageRequest.tag)) {
            // 将订单号存入map
            map.put("orderNumber", orderByOrderId.getOrderNumber());
            // 获取Driver的姓名
            String driverName = driver.getDriverName();
            // 取出Driver姓名的第一个字符
            String substring = driverName.substring(0, 1);
            // 根据Driver姓名的长度生成一定数量的星号
            String driverNames = substring + "*".repeat(substring.length() - 1);
            // 将生成的星号字符串存入map
            map.put("driverName", driverNames);
            // 创建StringSubstitutor对象用于替换字符串
            StringSubstitutor stringSubstitutor = new StringSubstitutor(map);
            // 使用StringSubstitutor替换shortMessageName中的占位符
            String replace = stringSubstitutor.replace(shortMessageName);
            // 设置Mailbox的名称为替换后的字符串
            messageRequests.setMailboxName(replace);
            // 设置Mailbox的ID为乘客电话
            messageRequests.setMailboxId(orderByOrderId.getPassengerPhone());
            // 将Mailbox对象转换为JSON字符串
            String jsonString = JSON.toJSONString(messageRequests);
            // 向MQ发送JSON字符串
            producer(jsonString);
            return;
        }

        if (TagCass.TAG_CASS_ID.getName().equals(messageRequest.tag)) {
            map.put("time", orderByOrderId.getOrderNumber());
            map.put("orderNumber", String.valueOf(orderByOrderId.getStartTime()));
            map.put("amount", String.valueOf(orderByOrderId.getOrderMoney()));
            StringSubstitutor stringSubstitutor = new StringSubstitutor(map);
            String replace = stringSubstitutor.replace(shortMessageName);
            messageRequests.setMailboxId(orderByOrderId.getPassengerPhone());
            messageRequests.setMailboxName(replace);
            // 将Mailbox对象转换为JSON字符串
            String jsonString = JSON.toJSONString(messageRequests);
            // 向MQ发送JSON字符串
            producer(jsonString);
            return;
        }

    }


    /**
     * 向MQ发送JSON字符串
     *
     * @param jsonString
     */
    public void producer(String jsonString) {
        rabbitTemplate.convertAndSend(RabbitMQConstants.ADD_LOG_QUEUE_NAME, jsonString, message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
        });
    }
}
